package com.im.app.controller;

import com.im.app.authConfig.CurrentUser;
import com.im.app.authConfig.RequireLogin;
import com.im.app.model.OfflineReply;
import com.im.app.model.User;
import com.im.app.service.IOfflineReplyService;
import com.im.app.utils.Response;
import com.im.app.utils.ResponseResult;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping(value = "/offlineReply")
public class OfflineReplyController {

    @Resource
    private IOfflineReplyService offlineReplyService;


    @GetMapping(value = "/queryOfflineReplyList")
    @RequireLogin
    public ResponseResult<List<OfflineReply>> queryOfflineReplyList(@CurrentUser User user) {
        //调用业务层返回列表
        OfflineReply offlineReply = new OfflineReply();
        offlineReply.setUserId(user.getUserId());
        List<OfflineReply> offlineReplyList = offlineReplyService.selectOfflineReplyList(offlineReply);
        return Response.makeOKRsp(offlineReplyList);
    }

    @GetMapping(value = "/getOfflineReplys")
    public ResponseResult<List<OfflineReply>> getOfflineReplys(String userId) {
        //调用业务层返回列表
        OfflineReply offlineReply = new OfflineReply();
        offlineReply.setUserId(userId);
        List<OfflineReply> offlineReplyList = offlineReplyService.selectOfflineReplyList(offlineReply);
        return Response.makeOKRsp(offlineReplyList);
    }


    @GetMapping(value ="/find")
    @RequireLogin
    public ResponseResult<OfflineReply> findOfflineReply(Integer id) {
        try {
            //调用业务层返回列表
            OfflineReply offlineReply = offlineReplyService.selectOfflineReplyById(Long.valueOf(id));
            return Response.makeOKRsp(offlineReply);
        } catch (Exception e) {
            e.printStackTrace();
            return Response.makeErrRsp("查询离线回复信息异常");
        }
    }

    @GetMapping(value ="/delete")
    @RequireLogin
    public ResponseResult deleteOfflineReply(@CurrentUser User user , Integer id) {
        if(user == null){
            return Response.makeErrRsp("用户不存在");
        }
        int result = offlineReplyService.deleteOfflineReplyById(user,Long.valueOf(id));
        if(result>0){
            return Response.makeOKRsp("删除成功");
        }else{
            return Response.makeErrRsp("删除失败");
        }

    }

    @PostMapping(value ="/add")
    @RequireLogin
    public ResponseResult<Object> addOfflineReply(@CurrentUser User user, @RequestBody OfflineReply offlineReply) {
        offlineReply.setUserId(user.getUserId());
        int result = offlineReplyService.insertOfflineReply(offlineReply);
        if(result>0){
            return Response.makeOKRsp("添加成功");
        }else{
            return Response.makeErrRsp("添加失败");
        }
    }

    @PostMapping(value ="/update")
    @RequireLogin
    public ResponseResult updateOfflineReply(@CurrentUser User user,@RequestBody OfflineReply offlineReply) {
        offlineReply.setUserId(user.getUserId());
        int result = offlineReplyService.updateOfflineReply(offlineReply);
        if(result>0){
            return Response.makeOKRsp("修改成功");
        }else{
            return Response.makeErrRsp("修改失败");
        }
    }








}
